<!--
 * @Author: cgl
 * @Date: 2023-04-18 15:12:26
 * @LastEditors: liangkun 2669681641@qq.com
 * @LastEditTime: 2023-12-08 09:02:17
 * @FilePath: \radiation-hygiene-fe\src\views\person\entrustOrganization.vue
 * @Description:
 *
 * Copyright (c) 2023 by cgl/微盛信息产业有限公司, All Rights Reserved.
-->
<template>
  <div class="app-container">
    <el-form
      class="searchform"
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="100px"
    >
      <el-row>
        <el-col :span="5">
          <el-form-item label="单位名称" label-width="80px">
            <!-- <el-input
							v-model="queryParams.organizationName"
							placeholder="请输入"
							clearable
							style="width: 90%"
							@keyup.enter.native="getList()"
						/> -->
            <m-select
              v-model="queryParams.organizationName"
              placeholder="输入文字检索查询"
              style="width: 100%"
              filterable
              clearable
              :lists="entrustOrganizationSelect"
              lists_label="organizationName"
              lists_value="organizationName"
              lists_key="entrustOrganizationId"
              :total="orgTotal"
              :pageSize="10"
              @handleChange="listeningEvent"
            >
            </m-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="单位编号">
            <el-input
              v-model="queryParams.unitNumber"
              placeholder="请输入"
              clearable
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="联系人/电话">
            <el-input
              v-model="queryParams.keywords"
              placeholder="请输入"
              clearable
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="单位类型">
            <el-select
              v-model="queryParams.organizationType"
              placeholder="请选择"
              style="width: 90%"
            >
              <el-option
                v-for="item in dict.type.organization_type"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="5">
          <el-form-item label="录入时间" label-width="80px">
            <el-date-picker
              v-model="queryParams.formDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              clearable
              style="width: 100%"
            >
            </el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="table-wrapper">
      <div class="tableoperation">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click.native.prevent="openDialog()"
          >添加</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-download"
          size="mini"
          @click.native.prevent="handleDownloadTemplate()"
          >下载模板</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-upload2"
          size="mini"
          plain
          @click.native.prevent="handleImport()"
          >导入</el-button
        >
      </div>

      <el-table
        v-loading="loading"
        :data="entrustOrganizationList"
        @row-dblclick="(row) => openDialog(row, '3')"
      >
        <el-table-column label="序号" type="index" width="50" align="center" />
        <el-table-column
          label="单位编号"
          align="center"
          prop="unitNumber"
          width="100"
        />
        <el-table-column
          label="单位名称"
          align="center"
          prop="organizationName"
          :show-overflow-tooltip="true"
        />
        <!-- <el-table-column label="法定代表人" align="center" prop="legalRepresentative" width="100"/> -->
        <el-table-column
          label="联系人"
          align="center"
          prop="contactsName"
          width="100"
        />
        <el-table-column
          label="联系电话"
          align="center"
          prop="contactsMobile"
          width="110"
        />
        <el-table-column
          label="单位地址"
          align="center"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{ scope.row.addressDetails }}
          </template>
        </el-table-column>
        <el-table-column label="单位类型" align="center">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.organization_type"
              :value="scope.row.organizationType"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column label="检测状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status_disable" :value="scope.row.detectionState"/>
        </template>
			</el-table-column> -->
        <el-table-column
          label="收费状态"
          align="center"
          prop="charge"
          width="110"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.is_or_no"
              :value="scope.row.charge || 0"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="合同状态"
          align="center"
          prop="agreementState"
          width="110"
        >
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.agreement_state"
              :value="scope.row.agreementState"
            />
          </template>
        </el-table-column>
        <el-table-column label="合同期限" align="center" width="180">
          <template slot-scope="scope">
            {{
              `${parseTime(scope.row.beginTime, "{y}-{m}-{d}") || ""} - ${
                parseTime(scope.row.endTime, "{y}-{m}-{d}") || ""
              }`
            }}
          </template>
        </el-table-column>
        <el-table-column label="监测状态" align="center" width="100">
          <template slot-scope="scope">
            <dict-tag
              :options="dict.type.status_disable"
              :value="scope.row.monitoringState"
            />
          </template>
        </el-table-column>
        <!-- <el-table-column label="用户状态" align="center" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.status_disable" :value="scope.row.userState"/>
        </template>
			</el-table-column> -->
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="150"
        />
        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="{ row }">
            <el-button @click.native.prevent="openDialog(row)" type="text">
              编辑
            </el-button>
            <el-button
              type="text"
              @click="handleResetPwd(row)"
              v-hasPermi="['system:user:resetPwd']"
              >重置密码</el-button
            >
            <el-button type="text" @click="handleRemove(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
      @close="closeUpload"
      v-dialogDrag="true"
    >
      <el-upload
        ref="upload"
        :limit="1"
        :on-exceed="handleExceed"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>仅允许导入xls、xlsx格式文件。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加委托单位弹框 -->
    <addOrganization
      ref="addOrganization"
      @getList="getList()"
    ></addOrganization>
  </div>
</template>

<script>
import {
  listUser,
  getUser,
  delUser,
  addUser,
  updateUser,
  resetUserPwd,
  changeUserStatus,
  deptTreeSelect,
  postOptionselect,
} from "@/api/system/user";
import {
  entrustOrganizationList,
  entrustOrganizationListing,
  entrustOrganizationExport,
  entrustOrganizationDetail,
  downloadTemplate,
  entrustOrganizationAdd,
  importData,
  entrustOrganizationEdit,
  entrustOrganizationDel,
  entrustOrganizationDelete,
  removeCompanyInfo,
} from "@/api/person/entrustOrganization";
import { getToken } from "@/utils/auth";
import addOrganization from "./addOrganization.vue";
import MSelect from "@/components/mSelect";
import { parseTime } from "@/utils/ruoyi";

export default {
  name: "User",
  dicts: ["status_disable", "organization_type", "agreement_state", "is_or_no"],
  components: { addOrganization, MSelect },
  data() {
    return {
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 单位查询数据
      organizationQueryData: {
        organizationName: "", //单位名称模糊查询
        pageNum: 1, //页码
        pageSize: 10, //每页展示的条数
      },
      orgTotal: 0,
      // 视图高度
      height: document.documentElement.clientHeight,
      // 委托单位列表
      entrustOrganizationList: [],
      loading: false,
      total: 0,
      // 委托单位下拉框
      entrustOrganizationSelect: [],
      // 导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url:
          process.env.VUE_APP_BASE_API +
          "/basic/entrust/organization/importData",
      },
    };
  },
  created() {
    this.getList();
  },
  filters: {
    ellipsis(value) {
      if (!value) return "";
      if (value.length > 12) {
        return value.slice(0, 12) + "...";
      }
      return value;
    },
  },
  methods: {
    // 获取委托单位列表
    getList() {
      this.queryParams.createTimeStart =
        this.queryParams.formDate && this.queryParams.formDate[0];
      this.queryParams.createTimeEnd =
        this.queryParams.formDate && this.queryParams.formDate[1];
      let queryParams = { ...this.queryParams };
      delete queryParams.formDate;
      this.loading = true;
      entrustOrganizationList(queryParams)
        .then((response) => {
          this.entrustOrganizationList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 获取单位下拉框
    listeningEvent(e) {
      let { queryName, pageNum, pageSize, type } = e;
      if (type == "scroll" && pageNum == this.organizationQueryData.pageNum)
        return;
      this.organizationQueryData.organizationName = queryName;
      this.organizationQueryData.pageNum = pageNum;
      this.organizationQueryData.pageSize = pageSize;
      entrustOrganizationList(this.organizationQueryData).then((response) => {
        if (type == "scroll") {
          this.entrustOrganizationSelect.push(...response.rows);
        } else {
          this.entrustOrganizationSelect = response.rows;
        }
        this.orgTotal = response.total;
      });
    },
    // 打开对话框
    openDialog(row, status) {
      this.form = row;
      this.resetForm("form");
      this.$refs.addOrganization.openDialog(row, status);
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm(`是否确认删除单位[${row.organizationName}]？`)
        .then(() => {
          this.loading = true;
          return entrustOrganizationDelete(row.entrustOrganizationId);
        })
        .then(() => {
          this.loading = false;
          this.$modal.msgSuccess("删除成功");
          this.getList();
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 下载模板
    handleDownloadTemplate() {
      this.download(
        "/basic/entrust/organization/downloadTemplate",
        {},
        `单位模板.xls`
      );
    },
    // 导出
    handleExport() {
      this.download(
        "/basic/entrust/organization/export",
        {
          ...this.queryParams,
        },
        `单位列表.xlsx`
      );
    },
    // 关闭导入
    closeUpload() {
      this.$refs.upload.clearFiles();
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log("response", response);
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    // 超出数量提示
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件`);
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      };
      this.resetForm("queryParams");
      this.getList();
    },
    // 重置密码按钮操作
    handleResetPwd(row) {
      this.$modal.confirm(`是否确认重置密码？`).then(() => {
        resetUserPwd(row.userId, "").then((response) => {
          this.$modal.msgSuccess("重置成功");
        });
      });
    },
    // 删除
    handleRemove(row) {
      this.$modal.confirm(`是否确认重置密码？`).then(async () => {
        removeCompanyInfo(row.entrustOrganizationId).then((response) => {
          this.$modal.msgSuccess("重置成功");
          this.getList();
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
